package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusBudgetAccountingTask;
import com.zhongwang.business.gen.entity.BusMaterialData;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AccPurImportModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.LaborTimeModel;
import com.zhongwang.business.util.BeanUtil;
import com.zhongwang.business.util.ShiroUtils;
import com.zhongwang.business.util.StringUtils;

import java.util.*;

/**
 * @author 徐一贺
 * @className AccPurImportListener
 * @description 外购件信息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class AccPurImportListener extends AnalysisEventListener<AccPurImportModel> {
  /** 钩子 */
  private ImportCallBack callBack;
  /** Excel相关参数模型 */
  private BusMaterialData param;
  /** 材料信息数据持久层 */
  private AccountingInfoDao accountingInfoDao;
  /** 错误列表 */
  private List<AccPurImportModel> errorList = new ArrayList<>();
  /** 持久化列表 */
  private List<AccPurImportModel> persistenceList = new ArrayList<>();
  /** 当前时间 */
  private Date currentTime = new Date();
  /** 专业部分信息 */
  private Map<String,List<String>> majorParts;

  /**
   * 构造函数
   *
   * @param param 页面参数
   * @param callBack 回调函数
   * @param accountingInfoDao 持久层
   */
  public AccPurImportListener(
          BusMaterialData param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
    this.callBack = callBack;
    this.param = param;
    this.accountingInfoDao = accountingInfoDao;
    majorParts = new HashMap<>();
    BusBudgetAccountingCost findPartListParam = new BusBudgetAccountingCost();
    findPartListParam.setBatId(param.getBatId());
    findPartListParam.setMajor("机械");
    List<String> mechanicsPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("流体");
    List<String> fluidPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("自动化");
    List<String> automationPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    majorParts.put("机械",mechanicsPart);
    majorParts.put("流体",fluidPart);
    majorParts.put("自动化",automationPart);
  }

  /**
   * 验证
   *
   * @param data 每行数据
   */
  @Override
  public void invoke(AccPurImportModel data, AnalysisContext context) {
    String errorInfo = "";
    boolean isRightMajor = false;
    if (data.getMajor() == null || StringUtils.trimAllBlank(data.getMajor()).equals("")) {
      errorInfo = "请填写专业！\r\n";
    }else if(data.getMajor()!= null && data.getMajor().length()>255){
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if(data.getMajor().equals("机械")||data.getMajor().equals("流体") || data.getMajor().equals("自动化")){
      isRightMajor=true;
    }else {
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if (data.getPart() == null || StringUtils.trimAllBlank(data.getPart()).equals("")) {
      errorInfo = "填写部分！\r\n";
    }else if(data.getPart()!= null && data.getPart().length()>255){
      errorInfo = "部分不可超过255个字符！\r\n";
    }else if(data.getPart()!= null && data.getMajor()!= null && isRightMajor && !data.getPart().equals("备品备件") && !data.getPart().equals("随机备件") && !data.getPart().equals("不可预见")){
      List<String> parts = majorParts.get(data.getMajor());
      if(!parts.contains(data.getPart())){
        errorInfo = "部分填写错误，该专业下无此部分！\r\n";
      }
    }
    if (data.getMatDesc() == null || StringUtils.trimAllBlank(data.getMatDesc()).equals("")) {
      errorInfo = "请填物料描述！\r\n";
    }else if(data.getMatDesc()!= null && data.getMatDesc().length()>255){
      errorInfo = "物料描述不可超过255个字符！\r\n";
    }
    if (data.getNum() == null) {
      errorInfo = "请填实用数量！\r\n";
    }
    if (data.getUnit() == null || StringUtils.trimAllBlank(data.getUnit()).equals("")) {
      errorInfo = "请填写单位！\r\n";
    }else if(data.getUnit()!= null && data.getUnit().length()>255){
      errorInfo = "单位不可超过255个字符！\r\n";
    }
    if (data.getPrice()== null) {
      errorInfo = "请填写单价！\r\n";
    }

    if (data.getStandardNo()!= null && data.getStandardNo().length()>255) {
      errorInfo = "型号/标准号不可超过255个字符！\r\n";
    }

    if (data.getBrand()!= null && data.getBrand().length()>255) {
      errorInfo = "品牌不可超过255个字符！\r\n";
    }

    if (errorInfo.length() > 0) {
      data.setErrorInfo(errorInfo);
      errorList.add(data);
    } else {
      persistenceList.add(data);
    }
  }

  /**
   * 持久化
   *
   * @param context
   */
  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    if(errorList.size()>0){
      callBack.onWarning(errorList);
    }else{
      Date currentDate  = new Date();
      BusBudgetAccountingTask order = accountingInfoDao.getBusBudgetAccountingTaskModelById(param.getBatId());
      for (int i = 0; i < persistenceList.size(); i++) {
        AccPurImportModel persistenceData = persistenceList.get(i);
        persistenceData.setBatId(param.getBatId());
        persistenceData.setDataUseType(2);
        persistenceData.setDataType(2);
        persistenceData.setGetDate(currentDate);
        persistenceData.setGetUid(ShiroUtils.getUserId());
        persistenceData.setPriceType(1);
        persistenceData.setProCode(order.getProCode());
        persistenceData.setBuyType("外购件");
        BusBudgetAccountingCost setCost = null;
        if(!persistenceData.getPart().equals("随机备件") && !persistenceData.getPart().equals("备品备件") && !persistenceData.getPart().equals("不可预见")){
          BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor()+"-"+persistenceData.getPart()+"-外购费");
          setCost = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
          if(setCost == null){
            int r = 1;
          }
        }else if(persistenceData.getPart().equals("随机备件")){
          BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setPart("随机备件");
          getBusBudgetAccountingCostCondition.setCode("随机备件");
          setCost = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        }else if(persistenceData.getPart().equals("备品备件")){
          BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setPart("备品备件");
          getBusBudgetAccountingCostCondition.setCode("备品备件");
          setCost = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        }else if(persistenceData.getPart().equals("不可预见")){
          BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setCode("不可预见费");
          setCost = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        }
        persistenceData.setCostId(setCost.getId());
        BusMaterialData data = new BusMaterialData();
        BeanUtil.copy(persistenceData,data);
        accountingInfoDao.insertBusMaterialData(data);
        setCost.setDataSource("1");
        accountingInfoDao.updateBusBudgetAccountingCostDataSourceByPrimaryKey(setCost);
      }
      callBack.onSuccess();
    }
  }

  /**
   * 异常处理
   *
   * @param exception 异常信息
   * @param context
   */
  @Override
  public void onException(Exception exception, AnalysisContext context) {

  }
}
